package com.iwayin.web.action.follow;

import java.util.List;

import com.iwayin.core.bo.follow.FollowBO;
import com.iwayin.core.bo.user.UserBO;
import com.iwayin.core.dataobject.FollowDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.exception.BOException;
import com.iwayin.web.action.BaseAction;
import com.iwayin.web.common.AjaxResult;

public class FollowAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4105465558513081948L;

	private FollowDO query;
	private List<UserDO> userList;
	private AjaxResult ajaxResult;
	private FollowBO followBO;
	private UserBO userBO;
	private UserDO user;

	/**
	 * @descriptions 添加关注
	 * @return
	 */
	public String addFollow() {
		UserDO loginUser = this.getLoginUser();
		if(loginUser==null){
			ajaxResult = AjaxResult.errorResult("请先登录");
			return SUCCESS;
		}
		try {
			query.setUserId(loginUser.getId());
			this.followBO.addFollow(query);
			ajaxResult = AjaxResult.succResult();
		} catch (BOException e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		} 
		return SUCCESS;
	}

	/**
	 * @descriptions 取消关注
	 * @return
	 */
	public String deleteFollow() {
		UserDO loginUser = this.getLoginUser();
		if(loginUser==null){
			ajaxResult = AjaxResult.errorResult("请先登录");
			return SUCCESS;
		}
		try {
			query.setUserId(loginUser.getId());
			this.followBO.deleteFollow(query);
			ajaxResult = AjaxResult.commonResult("取消关注成功！");
		} catch (BOException e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		} catch (Exception e) {
			ajaxResult = AjaxResult.errorResult("取消关注失败！");
		}
		return SUCCESS;
	}
	/**
	 * @descriptions 查询followList
	 * @return
	 */
	public String findFollowList() {
		user = userBO.getUserById(query.getUserId());
		Long userId = null;
		if (this.getLoginUser() != null) {
			userId = this.getLoginUser().getId();
		}
		userList = this.followBO.findFollowList(userId, query);
		return SUCCESS;
	}
	/**
	 * 未登录关注查询
	 * @return
	 */
	public String findNoLoginFollowList() {
		userList = this.followBO.findFollowList(null, query);
		return SUCCESS;
	}

	public FollowDO getQuery() {
		return query;
	}

	public void setQuery(FollowDO query) {
		this.query = query;
	}

	public List<UserDO> getUserList() {
		return userList;
	}

	public AjaxResult getAjaxResult() {
		return ajaxResult;
	}

	public void setFollowBO(FollowBO followBO) {
		this.followBO = followBO;
	}

	public UserDO getUser() {
		return user;
	}

	public void setUser(UserDO user) {
		this.user = user;
	}

	public void setUserBO(UserBO userBO) {
		this.userBO = userBO;
	}
	
}
